package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.Postage;
import cn.stylefeng.guns.sys.modular.tao.mapper.PostageMapper;
import cn.stylefeng.guns.sys.modular.tao.model.params.PostageParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.PostageResult;
import  cn.stylefeng.guns.sys.modular.tao.service.PostageService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 运费地区金额管理 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-24
 */
@Service
public class PostageServiceImpl extends ServiceImpl<PostageMapper, Postage> implements PostageService {

    @Override
    public void add(PostageParam param){
        Postage entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(PostageParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(PostageParam param){
        Postage oldEntity = getOldEntity(param);
        Postage newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public PostageResult findBySpec(PostageParam param){
        return null;
    }

    @Override
    public List<PostageResult> findListBySpec(PostageParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(PostageParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(PostageParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Postage getOldEntity(PostageParam param) {
        return this.getById(getKey(param));
    }

    private Postage getEntity(PostageParam param) {
        Postage entity = new Postage();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
